package dreamCookGame;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioClipBig {
	String _name;
	BigClip _soundClip;
	
	AudioInputStream ais;
	URL url;
	
	public AudioClipBig(String name) {
		_name = name;
	}

	public void loadResouces(References reference) {
		try {
			File soundFile = reference.getAudioFile(_name);
			url = soundFile.toURL();
			ais = AudioSystem.getAudioInputStream(url);
			AudioFormat af = ais.getFormat();
			_soundClip = new BigClip();
			_soundClip.open(ais);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(LineUnavailableException e) {
			e.printStackTrace();
		}
		
	}

	public void play() {
		_soundClip.setFramePosition(0);
		//_soundClip.start();
		if(!_soundClip.isRunning() && !_soundClip.isActive())
			_soundClip.loop(1);
		//System.out.println("is active: " + _soundClip.isActive());
		//System.out.println("is open: " + _soundClip.isOpen());
		//System.out.println("is running: " + _soundClip.isRunning());
	}

	
	public void stop(){
		_soundClip.setFramePosition(0);
		
		if(_soundClip.isRunning()) {
			_soundClip.flush();
			_soundClip.stop();
		}
		
	}
	
	public void loop() {
		// TODO Auto-generated method stub

		_soundClip.setFramePosition(0);
		_soundClip.loop(3);

	}
	 
}
